package com.itzxx.huazhun.ui.record.curve

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.github.aachartmodel.aainfographics.aachartcreator.AASeriesElement
import com.itzxx.huazhun.data.model.DetailedList
import com.itzxx.huazhun.data.model.DevicesChildBean
import com.itzxx.huazhun.data.repository.HttpRequestCoroutine
import com.itzxx.huazhun.ui.device.list.DevicesListActivity
import com.itzxx.library_base_ktx.base.viewmodel.BaseViewModel
import com.itzxx.library_base_ktx.callback.databind.BooleanObservableField
import com.itzxx.library_base_ktx.callback.databind.StringObservableField
import com.itzxx.library_base_ktx.callback.livedata.event.EventLiveData
import com.itzxx.library_base_ktx.ext.request
import com.itzxx.library_base_ktx.state.ListDataUiState

class CurveAnalysislViewModel : BaseViewModel() {

    var recordId = ""
    var devicesId = 0
    var equipmentNo = ""

    val curveDataLiveData = MutableLiveData<ArrayList<AASeriesElement>>()
    val curveDataCategoriesLiveData = MutableLiveData<ArrayList<String>>()
    val curveDataMaxValueObservableField = StringObservableField()

    //列表数据刷新
    val listDataUiStateLiveData = EventLiveData<ListDataUiState<DetailedList>>()

    // 设备详情id
    val devicesInfoLiveData = EventLiveData<DevicesChildBean>()

    //是否展示设备介绍
    val isVisibleDetailObservableField = BooleanObservableField(false)


    private var cursor: String = ""

    /**
     * 采集数据曲线
     */
    fun queryEquipmentDataCurve(id: String) {
        recordId = id
        loadingChange.showDialog.postValue("数据加载中...")
        request({
            HttpRequestCoroutine.queryEquipmentDataCurve(
                recordId
            )
        }, {
            //底部最大数值说明
            curveDataMaxValueObservableField.set(it.maxValues)
            if (it.data.isNotEmpty()) {
                val data = it.data[0]
                //底部标签
                val arrayCategories = arrayListOf<String>()
                it.data.forEach { item ->
                    arrayCategories.add(item.times)
                }
                curveDataCategoriesLiveData.postValue(arrayCategories)
                //具体点数据
                val arrayType = arrayListOf<AASeriesElement>()
                //添加气压参数
                if (!TextUtils.isEmpty(data.pressure) && !TextUtils.equals("0.0", data.pressure)) {
                    val arrayQiya = arrayListOf<Array<Any>>()
                    it.data.forEach { item ->
                        arrayQiya.add(arrayOf(item.times, item.pressure.toDouble()))
                    }
                    arrayType.add(
                        AASeriesElement()
                            .name("气压")
                            .data(arrayQiya.toArray())
                    )
                }

                if (!TextUtils.isEmpty(data.temperature) && !TextUtils.equals(
                        "0.0",
                        data.temperature
                    )
                ) {
                    val arrayWendu = arrayListOf<Array<Any>>()
                    it.data.forEach { item ->
                        arrayWendu.add(arrayOf(item.times, item.temperature.toDouble()))
                    }
                    arrayType.add(
                        AASeriesElement()
                            .name("温度")
                            .data(arrayWendu.toArray())
                    )
                }
                curveDataLiveData.postValue(arrayType)
            }
            loadingChange.dismissDialog.postValue(false)
        }, {
            toast(it.errorMsg)
            loadingChange.dismissDialog.postValue(false)
        })
        queryEquipmentDataCurve(true)
    }

    /**
     * 采集数据曲线
     */
    fun queryEquipmentDataCurve(isRefresh: Boolean) {
        request({
            HttpRequestCoroutine.queryMeasurementDataDetailsPage(
                recordId,
                cursor,
            )
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = true,
                    isRefresh = isRefresh,
                    isEmpty = it.list.isEmpty(),
                    hasMore = !it.isLast,
                    isFirstEmpty = TextUtils.isEmpty(cursor) && it.list.isEmpty(),
                    listData = it.list
                )
            cursor = it.cursor
            listDataUiStateLiveData.postValue(listDataUiState)
        }, {
            toast(it.errorMsg)
        })
    }


    fun requestDetail(devicesId: Int, equipmentNo: String?) {
        equipmentNo?.let {
            this.devicesId = devicesId
            this.equipmentNo = equipmentNo
            request({
                HttpRequestCoroutine.detectionEquipmentInfo(
                    devicesId,
                    equipmentNo
                )
            }, {
                isVisibleDetailObservableField.set(true)
                devicesInfoLiveData.postValue(it)
            }, {
                toast(it.errorMsg)
            })
        } ?: isVisibleDetailObservableField.set(false)
    }

    fun addDevices() {
        baseContext.value?.let { DevicesListActivity.startActivity(it, recordId) }
    }
}